﻿using System;
using WPNextTools.NBase;
using WPNextTools.NCommon;

namespace WPNextTools.NControls
{
    internal class ExplodeVirtualizationStrategy : VirtualizationStrategy
    {
        protected override bool CaculateItemParams(IDataSourceItem dataSourceItem)
        {
            int lowestIndex = GetLowestIndex();
            if (dataSourceItem.Value is IImageModel)
            {
                double width = GetColumnWidth();
                double height = width * ((IImageModel)dataSourceItem.Value).ImgRatio;
                double x = (columnMarginCache + width) * lowestIndex + base.leftRightMarginCache;
                double y = columnY[lowestIndex] + columnMarginCache;
                if (columnY[lowestIndex] == 0.0)
                {
                    y = 0.0;
                }
                dataSourceItem.Width = width;
                dataSourceItem.Height = height;
                dataSourceItem.X = x;
                dataSourceItem.Y = y;
                dataSourceItem.ColumnIndex = lowestIndex;
                columnY[lowestIndex] = y + height;

                double virtualY = y - this.owner.manipulationContainer.VerticalOffset;
                if (virtualY >= -NEnviroment.ScrreenHeight && virtualY <= NEnviroment.ScrreenHeight * 2)
                {
                    return true;
                }
            }
            else
            {
                throw new InvalidOperationException("It's no implements IImageModel!");
            }
            return false;
        }
    }
}
